package com.wangdao.mall.controller.client;

import com.google.gson.Gson;
import com.wangdao.mall.model.Password;
import com.wangdao.mall.model.Result;
import com.wangdao.mall.model.User;
import com.wangdao.mall.service.AdminService;
import com.wangdao.mall.service.AdminServiceImp;
import com.wangdao.mall.service.UserService;
import com.wangdao.mall.service.UserServiceImp;
import com.wangdao.mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:HaiyangTian
 * @Description:
 * @Date: Created in 20:12 2020/5/5
 * @Modified By:
 */
@WebServlet("/api/mall/user/*")
public class UserServlet extends HttpServlet {

    private UserService userService = new UserServiceImp();

    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String action = uri.replace("/api/mall/user/", "");
        if ("login".equals(action)){
            login(request,response);
        }else if ("signup".equals(action)){
            signup(request,response);
        }else if ("updateUserData".equals(action)){
            updateUserData(request,response);
        }else if ("updatePwd".equals(action)){
            updatePwd(request,response);
        }
    }

    private void updatePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Password password = gson.fromJson(requestBody, Password.class);
        Result result = new Result();
        if (password.getOldPwd().isEmpty() || password.getNewPwd().isEmpty() || password.getConfirmPwd().isEmpty()){
            result.setCode(10000);
            result.setMessage("密码不能为空，请重新输入");
            response.getWriter().println(gson.toJson(result));
            return;
        }else if (password.getOldPwd().equals(password.getNewPwd())){
            result.setCode(10000);
            result.setMessage("新密码不能与原密码相同,请重新输入");
            response.getWriter().println(gson.toJson(result));
            return;
        }else if (!password.getNewPwd().equals(password.getConfirmPwd())){
            result.setCode(10000);
            result.setMessage("两次密码输入不一致,请重新输入");
            response.getWriter().println(gson.toJson(result));
            return;
        }
        int row = userService.updatePwd(password);
        if (row == 1){
            //修改成功
            result.setCode(0);
        }else {
            //修改错误
            result.setCode(10000);
            result.setMessage("原密码输入错误，请重新输入");
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 更新用户数据
     * @param request
     * @param response
     */
    private void updateUserData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        User user = gson.fromJson(requestBody, User.class);
        userService.updateUserData(user);
        Result result = new Result();
        result.setCode(0);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 用户注册
     * @param request
     * @param response
     */
    private void signup(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        User user = gson.fromJson(requestBody, User.class);
        Result result = new Result();
        int row = userService.signup(user);
        Map<Object, Object> map = new HashMap<>();
        if (row == 1){
            map.put("code",0);
            map.put("name",user.getNickname());
            map.put("token",user.getNickname());
            result.setCode(0);
            result.setData(map);
        }else{
            result.setCode(10000);
            result.setMessage("邮箱昵称已被使用，请重新输入");
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 客户端登陆
     * @param request
     * @param response
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        User user = gson.fromJson(requestBody, User.class);
        Result result = new Result();
        int code = userService.login(user);
        if (code == 200){
            HttpSession session = request.getSession();
            session.setAttribute("email",user.getEmail());
            Map<Object, Object> map = new HashMap<>();
            map.put("token",user.getNickname());
            map.put("name",user.getNickname());
            result.setCode(0);
            result.setData(map);
        }else{
            result.setCode(10000);
            result.setMessage("用户名或密码不正确，请重新输入");
        }
        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String action = uri.replace("/api/mall/user/", "");
        if ("data".equals(action)){
            getUserData(request,response);
        }
    }

    /**
     * 获取用户信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void getUserData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String token = request.getParameter("token");
        User user = userService.getUserData(token);
        Result result = new Result();
        result.setCode(0);
        result.setData(user);
        response.getWriter().println(gson.toJson(result));
    }
}
